package com.ego.controller;

import com.ego.commons.pojo.CartPojo;
import com.ego.commons.pojo.EgoResult;
import com.ego.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;
    //添加商品到购物车
    @RequestMapping("/cart/add/{id}.html")
    public String addCart(@PathVariable Long id, int num){
        cartService.cartAdd(id,num);
        return "cartSuccess";
    }

    //购物车列表
    @RequestMapping("/cart/cart.html")
    public String cartList(Model model){
        List<CartPojo> cartPojoList = cartService.cartList();

        model.addAttribute("cartList",cartPojoList);
        return "cart";
    }
    //删除
    @RequestMapping("/cart/delete/{id}.action")
    @ResponseBody
    public EgoResult cartList(@PathVariable Long id){

        return cartService.deleteCartItemByItemId(id);
    }
    //修改
    @RequestMapping({"/service/cart/update/num/{id}/{num}","/cart/update/num/{id}/{num}.action"})
    @ResponseBody
    public EgoResult updateCartItemNum(@PathVariable long id,@PathVariable int num){
        return cartService.updateCartItemNum(id,num);
    }
    //结算页面
    @RequestMapping("/cart/order-cart.html")
    public String cartOrderCart(@RequestParam List<Long> id, Model model) {
        List<CartPojo> cartPojos = cartService.orderCartByIds(id);

        model.addAttribute("cartList", cartPojos);
        return "order-cart";
    }
}
